import KPlayer from './player'

/**
 * ui界面类.
 */
export default class KUi {
  public id: string;
  public player: KPlayer;

  /**
   * @param id 控件id
   * @param player 玩家
   */
  constructor(id: string, player: KPlayer) {
    this.id = id
    this.player = player
  }

  /**
   * 根据界面模块创建ui
   * @deprecated 暂不可用
   * @param player
   * @param id 模板id
   * @param pid 父级Id
   */
  static createByModule(player: KPlayer, id: string, pid: string): KUi {
    const ui = gameapi.create_ui_prefab_instance(player.handle, id, pid)
    return new KUi('', player)
  }

  /**
   * 搜索路径的控件
   * @param player
   * @param path
   */
  static searchPath(player: KPlayer, path: string[]): KUi {
    const uid = gameapi.get_comp_by_absolute_path(player.handle, path.join('.'))
    if (uid == null) {
      logUtil.error('ui控件不存在: ' + path.join('.'))
      return null
    }
    return new KUi(<string>uid, player)
  }

  /**
   * 控件名.
   */
  get name(): string {
    return <string>gameapi.get_ui_comp_name(this.player.handle, this.id)
  }

  /**
   * 父级控件.
   */
  get parent(): KUi {
    return new KUi(this.parentId, this.player)
  }

  /**
   * 父级控件id.
   */
  get parentId(): string {
    return <string>gameapi.get_ui_comp_parent(this.player.handle, this.id)
  }

  /**
   * 开启.
   */
  open() {
    gameapi.set_ui_comp_enable(this.player.handle, this.id, true)
  }

  /**
   * 设置禁用.
   */
  disable(value: boolean) {
    gameapi.set_ui_comp_enable(this.player.handle, this.id, !value)
  }

  /**
   * 显示/隐藏
   * @param value
   */
  visible(value: boolean) {
    gameapi.set_ui_comp_visible(this.player.handle, value, this.id)
  }

  /**
   * 设置文本
   * @param value
   */
  setText(value: string) {
    gameapi.set_ui_comp_text(this.player.handle, this.id, value)
  }

  /**
   * 设置图片
   * @param value 图片id
   */
  setImage(value: number) {
    gameapi.set_ui_comp_image(this.player.handle, this.id, value)
  }

  /**
   * 设置坐标
   * @param x
   * @param y
   */
  setPos(x: number, y: number) {
    gameapi.set_ui_comp_pos(this.player.handle, this.id, x, y)
  }

  // textColor(player: KPlayer) {
  //
  // }
}
